package com.lianqi.emcpframework.data.masker.stratgey;
import com.lianqi.emcpframework.data.masker.annotation.MaskData;
import com.lianqi.emcpframework.utils.StringUtils;

/**
 * @program emcp-framework
 * @ClassName EmailMaskStrategy
 * @description: 电子邮箱脱敏策略实现类
 * @author: sky
 * @create: 2019/05/26 19:29
 */
public class EmailMaskStrategy implements DataMaskStrategy<String>{


    @Override
    public int defaultMaskLeft() {
        return 1;
    }

    @Override
    public int defaultMaskRight() {
        return 0;
    }

    @Override
    public String maskValue(Object value,MaskData maskData) {
        if (StringUtils.isNullOrEmpty(value)){
            return null;
        }
        int maskleft = defaultMaskLeft();
        int maskright = defaultMaskRight();
        String maskChar = DEFAULT_MASK_CHAR;
        if (maskData != null){
            maskChar = maskData.maskCharacter();
        }
        String oldValue = value.toString();
        int index = oldValue.indexOf("@");
        if ( index <= 1 ){
            return oldValue;
        }
        String newValue = org.apache.commons.lang3.StringUtils.rightPad(
                org.apache.commons.lang3.StringUtils.left(oldValue, maskleft), index, "*").
                concat(org.apache.commons.lang3.StringUtils.mid(oldValue, index, oldValue.length()));
        return newValue;
    }

}
